package com.yzt.zhuju.voice;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.CountDownTimer;
import android.text.TextUtils;

import com.yzt.zhuju.event.RxBus;
import com.yzt.zhuju.event.voice.VoiceEvent;

import org.greenrobot.eventbus.EventBus;

/**
 * author : bob
 * date   : 2022/11/29
 * desc   : 语音管理器
 */
public class VoicePlayManager {

    private MediaPlayer mVoiceMedia;

    private CountDownTimer mCountDownTimer;

    private int mTotalTime;
    private String mCurrentPlayId = "";
    private boolean isPlaying = false;
    private static VoicePlayManager mInstance;

    private VoicePlayManager() {
    }

    public static VoicePlayManager getInstance() {
        if (mInstance == null) {
            mInstance = new VoicePlayManager();
        }
        return mInstance;
    }

    public void voicePlay(Context context, String playId, String url) {
        if (url == null || TextUtils.isEmpty(url)) {
            return;
        }
        if (mCurrentPlayId.equals(playId)) {
            stopAlarm(playId);
            return;
        } else {
            stopAlarm(mCurrentPlayId);
            mCurrentPlayId = playId;
        }
        mVoiceMedia = MediaPlayerManage.getInstance().startAlarm(
                context,
                false,
                url, new MediaPlayerManage.VoicePlayListener() {

                    @Override
                    public void completePlay(MediaPlayer mp) {
                        stopAlarm(mCurrentPlayId);
                    }

                    @Override
                    public void startPlay(MediaPlayer mp) {
                        mTotalTime = mp.getDuration() / 1000;
                        isPlaying = true;
                        startAlarm(playId, mp);
                    }
                });
    }

    public void stopPlay() {
        stopAlarm(mCurrentPlayId);
    }

    private void stopAlarm(String playId) {
        isPlaying = false;
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
        }
        MediaPlayerManage.getInstance().stopAlarm(mVoiceMedia);
        EventBus.getDefault().post(new VoiceEvent(VoiceEvent.TYPE_STOP, playId, mTotalTime));
        RxBus.getDefault().post(new VoiceEvent(VoiceEvent.TYPE_STOP, playId, mTotalTime));
        mCurrentPlayId = "";
    }

    public boolean isPlaying() {
        return isPlaying;
    }

    private void startAlarm(String playId, MediaPlayer mediaPlayer) {
        mediaPlayer.start();
        int countTime = 1000;
        EventBus.getDefault().post(new VoiceEvent(VoiceEvent.TYPE_START, playId, mTotalTime));
        RxBus.getDefault().post(new VoiceEvent(VoiceEvent.TYPE_START, playId, mTotalTime));
        mCountDownTimer = new CountDownTimer(mediaPlayer.getDuration(), countTime) {

            @Override
            public void onTick(long millisUntilFinished) {
                EventBus.getDefault().post(new VoiceEvent(VoiceEvent.TYPE_TIME, playId, mTotalTime, (int) (millisUntilFinished / 1000)));
                RxBus.getDefault().post(new VoiceEvent(VoiceEvent.TYPE_TIME, playId, mTotalTime, (int) (millisUntilFinished / 1000)));
            }

            @Override
            public void onFinish() {
            }
        }.start();
    }

    public String getCurrentPlayId() {
        return mCurrentPlayId;
    }
}
